/** 
 *  map对象类,提供了put,get,remove,size,isEmpty等方法 
 */  
function CoreMap() {  
 /** 
  * 构造函数 
  * @param {} key 
  * @param {} value 
  */  
 var struct = function(key, value) {  
  this.key = key;  
  this.value = value;  
 }  
 /** 
  * 添加key对应的值，如果key已经存在则直接更新对应的值 
  * @param {} key 
  * @param {} value 
  */  
 var put = function(key, value) {  
  for (var i = 0; i < this.arr.length; i++) {  
   if (this.arr[i].key === key) {  
    this.arr[i].value = value;  
    return;  
   }  
  }  
  this.arr[this.arr.length] = new struct(key, value);  
 }  
 /** 
  * 获取key对应的值 
  * @param {} key 
  * @return {} 
  */  
 var get = function(key) {  
  for (var i = 0; i < this.arr.length; i++) {  
   if (this.arr[i].key === key) {  
    return this.arr[i].value;  
   }  
  }  
  return null;  
 }  
 /** 
  * 移除key对应的值 
  * @param {} key 
  */  
 var remove = function(key) {  
  var v;  
  for (var i = 0; i < this.arr.length; i++) {  
   v = this.arr.pop();  
   if (v.key === key) {  
    continue;  
   }  
   this.arr.unshift(v);  
  }  
 }  
 /** 
  * 获取map对象的长度 
  * @return {} 
  */  
 var size = function() {  
  return this.arr.length;  
 }
 
 var vArray = function(){
  return this.arr;
 }  
 /** 
  * 判断map对象是否为空 
  * @return {} 
  */  
 var isEmpty = function() {  
  return this.arr.length <= 0;  
 } 
  
 this.arr = new Array();  
 this.get = get;  
 this.put = put;  
 this.remove = remove;  
 this.size = size;  
 this.isEmpty = isEmpty;  
}  ;

/**
 *  FormatNumber(23.25666666666, 4) 23.2667
 * @param {} srcStr
 * @param {} nAfterDot
 * @returns {} 
 */
function FormatNumber(srcStr, nAfterDot) {
	var srcStr, nAfterDot;
	var resultStr, nTen;
	srcStr = "" + srcStr + "";
	strLen = srcStr.length;
	dotPos = srcStr.indexOf(".", 0);
	if (dotPos == -1) {
		resultStr = srcStr + ".";
		for (i = 0; i < nAfterDot; i++) {
			resultStr = resultStr + "0"
		}
		return resultStr
	} else {
		if ((strLen - dotPos - 1) >= nAfterDot) {
			nAfter = dotPos + nAfterDot + 1;
			nTen = 1;
			for (j = 0; j < nAfterDot; j++) {
				nTen = nTen * 10
			}
			resultStr = Math.round(parseFloat(srcStr) * nTen) / nTen;
			return resultStr
		} else {
			resultStr = srcStr;
			for (i = 0; i < (nAfterDot - strLen + dotPos + 1); i++) {
				resultStr = resultStr + "0"
			}
			return resultStr
		}
	}
}

function formatFloat(src, pos)
{
    return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
}

//get div html value
function GetDivValue(id){
	return document.getElementById(id).innerHTML;
}

function SetDivValue(id,value){
	return document.getElementById(id).innerHTML=value;
}

//get input value
function GetInputValue(id){
	return document.getElementById(id).value;
}

//create table rows
function CreateTableRows(TableName,rowIndex,ColumnArray){
	 var objRow =  document.getElementById(TableName).insertRow(rowIndex);
	
	 for(var i=0;i<ColumnArray.length;i++){
	 	objRow.insertCell(i).innerHTML=ColumnArray[i];
	 }
}

function CreateTableRowsWithCss(TableName,rowIndex,ColumnArray,className){
	 var objRow =  document.getElementById(TableName).insertRow(rowIndex);
	 objRow.className=className;
	 for(var i=0;i<ColumnArray.length;i++){
	 	objRow.insertCell(i).innerHTML=ColumnArray[i];
	 }
}

//setting button  style (disable or no)
function SettingButtonStyle(id,isShow){
	document.getElementById(id).disabled=isShow;
}

/**
 * 
 * @param {} id
 * @param {} isShow  ''显示;'none;'不显示
 * @returns {} 
 */
function SettingDivStyle(id,isShow){
	document.getElementById(id).style.display=isShow;
}

//setting html properties value
function SetValue(id,Value){
	document.getElementById(id).value=Value;
}

function SetValueByFrom(DOM,Value){
	DOM.value=Value;
}

function GetDOMById(formId){
	return document.getElementById(formId);
}

//submit form
function SubmitForm(formId,action){
	if(action!=null){
		document.getElementById(formId).action = action;
	}
	document.getElementById(formId).submit();
}

//delete column data
function RemoveRowsByTableName(tablename)
{	
	var U= document.getElementById(tablename).rows.length;
	for(var i=0;i<U;i++){
		 try{
		 	document.getElementById(tablename).deleteRow(0);
		 }catch(e){alert("异常捕获:"+e)}
	}
}

//all checked 
function AllCheckedByCheckBoxName(formname,checkboxname) {
    var thisform = document.getElementById(formname);
    var ids = document.getElementsByName(checkboxname);
    var isChecked = false;
    var mapv = new CoreMap();
    
    if (thisform.num.checked == true) {
        for (var i = 0; i < ids.length; i++) {
            ids[i].checked = true;
        }
    } else {
        for (var i = 0; i < ids.length; i++) {
            ids[i].checked = false;
        }
    }
    
    //mapcount();
}

function AllCheckedByCheckBoxNamePutValueByAutoAllCheck(formname,checkboxname,vMap,nameArray){
     var ids = document.getElementsByName(checkboxname);
    for (var i = 0; i < ids.length; i++) {
        ids[i].checked = true;
    }
   
    //put value
    for(var m=0;m<ids.length;m++){
    	var key = ids[m].value.toString();
    	var lineValue = new Array(); 
	    for(var k=0;k<nameArray.length;k++){
	    	var inputtype = nameArray[k].split(':');
	    	var id =  inputtype[0] + key;
	    	if('div'==inputtype[1] || 'span'==inputtype[1] ){
	    		lineValue[k] = GetDivValue(id.toString());
	    	}else if('input'==inputtype[1]){
	    		lineValue[k] = GetInputValue(id.toString());
	    	}
	    }
    	vMap.put(key,lineValue);
	}
	return vMap;
}

function AllCheckedByCheckBoxNamePutValue(formname,checkboxname,vMap,nameArray) {
    var thisform = document.getElementById(formname);
    var ids = document.getElementsByName(checkboxname);
    var isChecked = false;
  	//all checked 
    if (thisform.num.checked == true) {
        for (var i = 0; i < ids.length; i++) {
            ids[i].checked = true;
        }
        
        //put value
        for(var m=0;m<ids.length;m++){
	    	var key = ids[m].value.toString();
	    	var lineValue = new Array(); 
		    for(var k=0;k<nameArray.length;k++){
		    	var inputtype = nameArray[k].split(':');
		    	var id =  inputtype[0] + key;
		    	if('div'==inputtype[1]){
		    		lineValue[k] = GetDivValue(id.toString());
		    	}else if('input'==inputtype[1]){
		    		lineValue[k] = GetInputValue(id.toString());
		    	}
		    }
	    	vMap.put(key,lineValue);
    	}
    } else {
        for (var i = 0; i < ids.length; i++) {
            ids[i].checked = false;
        }
        //remove value
		for(var m=0;m<ids.length;m++){
			var key = ids[m].value.toString();
			vMap.remove(key);
		}
    }
    //mapcount();
    
    return vMap;
}

function dictionaryInit(){
	var dictionary = new CoreMap();
	dictionary.put('00140001','证大岁悦');
	dictionary.put('00140002','证大季喜');
	dictionary.put('00140003','证大年丰');
	dictionary.put('00140004','证大月收');
	dictionary.put('00140005','证大月定投');
	dictionary.put('00120001','1日');
	dictionary.put('00120002','16日');
	dictionary.put('1日','01');
	dictionary.put('16日','16');
	dictionary.put('02000005','初次撮合');
	dictionary.put('02000006','初次订购');
	dictionary.put('02000007','16日');
	dictionary.put('02000008','投资生效');
	dictionary.put('02000009','回款撮合');
	dictionary.put('02000010','回款订购');
	dictionary.put('02000011','投资结束');
	dictionary.put('03000001','新建');
	dictionary.put('03000002','通过');
	dictionary.put('03000003','拒绝');
	dictionary.put('03000004','撤销');
	dictionary.put('03000005','订购');
	dictionary.put('03000006','撤单');
	dictionary.put('03000007','部分付款');
	dictionary.put('03000008','全部付款');
	dictionary.put('03000009','已收款');
	dictionary.put('04000001','新建');
	dictionary.put('04000002','通过');
	dictionary.put('04000004','部分收款');
	dictionary.put('04000005','全部收款');
	dictionary.put('04000006','已取现');
	dictionary.put('04000007','撤销');
	dictionary.put('00080001','中国银行');
	dictionary.put('00080002','招商银行');
	dictionary.put('00080003','工商银行');
	dictionary.put('00080004','农业银行');
	dictionary.put('00080005','建设银行');
	dictionary.put('00080006','交通银行');
	dictionary.put('00080007','深圳发展银行');
	dictionary.put('00080008','浦发银行');
	dictionary.put('00080009','民生银行');
	dictionary.put('00080010','华夏银行');
	dictionary.put('00080011','兴业银行');
	dictionary.put('00080012','中信银行');
	dictionary.put('00080013','光大银行');
	dictionary.put('00080014','平安银行');
	dictionary.put('1001','随房贷');
	dictionary.put('1002','随意贷');
	dictionary.put('1003','随薪贷');
	dictionary.put('1004','随车贷');
	dictionary.put('1005','其他');
	return dictionary;
}

function showErrorMsg(errorinfo,thisform,errormsg_show_div_id,errormsg_info_div_id){
	if(errorinfo!=null && errorinfo!=''){
		SettingDivStyle(errormsg_show_div_id,'');
		SetDivValue(errormsg_info_div_id,errorinfo);
		return true;
	}else{
		SettingDivStyle(errormsg_show_div_id,'none');
		SetDivValue(errormsg_info_div_id,'');
		return false;
	}
}


function  createTableWithArray(data){
	return data;
}

function OnclickPutMapV(){
	for(var m=0;m<ids.length;m++){
	    	var key = ids[m].value.toString();
	    	var lineValue = new Array(); 
		    for(var k=0;k<nameArray.length;k++){
		    	var inputtype = nameArray[k].split(':');
		    	var id =  inputtype[0] + key;
		    	if('div'==inputtype[1]){
		    		lineValue[k] = GetDivValue(id.toString());
		    	}else if('input'==inputtype[1]){
		    		lineValue[k] = GetInputValue(id.toString());
		    	}
		    }
	    	vMap.put(key,lineValue);
    	}
}

function checkCheckBoxHasChecked(checkboxname){
	var ids = document.getElementsByName(checkboxname);
	for (var i = 0; i < ids.length; i++) {
		if(ids[i].checked==true){
			return true;
		}
    }
    return false;
}

function getDateFromString(StringDate){
  var date = StringDate.split('-');
  var myDate1 = new Date(date[0],date[1],date[2]);
  return myDate1;
}

/* Compare Date
startdate: 'yyyy-MM-dd'
enddate: 'yyyy-MM-dd'
return true or false
*/
function compareDate(startdate,enddate){
	var date1 = getDateFromString(startdate);
	var date2 = getDateFromString(enddate);
	return date1<date2;
}

function CheckBoxAlreadyChecked(checkboxname,numname){
	 var ids = document.getElementsByName(checkboxname);
	 var isTrue=true;
	 for(var m=0;m<ids.length;m++){
	    if(ids[m].checked!=true){
	    	isTrue = false;
	    }
     }
     
     document.getElementById(numname).checked = isTrue;
}


function formatCurrency(num) {
	if(num==null){
		return 0;
	}
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-')  + num + '.' + cents);
}

/**
 * 获取12.333444  == 12.33
 * @param {} v
 * @returns {} 
 */
function interceptionDouble(v){
  	 var amt_ = v.toString().split(".");
  	 var amt_1 = 0;
  	 if(amt_.length==2 && amt_[1].length>=2 ){
  	 	 amt_1 = amt_[0]+'.'+amt_[1].substring(0,2);
  	 }else{
  	 	amt_1 = v;
  	 }
  	 return amt_1;
}

function interceptionDoubleL(v,L){
  	 var amt_ = v.toString().split(".");
  	 var amt_1 = 0;
  	 if(amt_.length==2 && amt_[1].length>=L ){
  	 	 amt_1 = amt_[0]+'.'+amt_[1].substring(0,L);
  	 }else{
  	 	amt_1 = v;
  	 }
  	 return amt_1;
}



/**
 * Put选择CheckBox值(行值)
 * 
 */
function EditTableOnclickPutValue(checkboxname,vMap,nameArray){
	var ids = document.getElementsByName(checkboxname);
	for(var m=0;m<ids.length;m++){
    	if(ids[m].checked==true){
	    	var key = ids[m].value.toString();
	    	var lineValue = new Array(); 
		    for(var k=0;k<nameArray.length;k++){
		    	var inputtype = nameArray[k].split(':');
		    	var id =  inputtype[0] + key;
		    	if('div'==inputtype[1] || 'span'==inputtype[1] ){
		    		lineValue[k] = GetDivValue(id.toString());
		    	}else if('input'==inputtype[1]){
		    		lineValue[k] = GetInputValue(id.toString());
		    	}
		    }
	    	vMap.put(key,lineValue);
    	}
	}
	
	return vMap;
}

function EditTablePutValue(vMap,nameArray,key){
	var lineValue = new Array(); 
    for(var k=0;k<nameArray.length;k++){
    	var inputtype = nameArray[k].split(':');
    	var id =  inputtype[0] + key;
    	if('div'==inputtype[1] || 'span'==inputtype[1] ){
    		lineValue[k] = GetDivValue(id.toString());
    	}else if('input'==inputtype[1]){
    		lineValue[k] = GetInputValue(id.toString());
    	}
    }
	vMap.put(key,lineValue);
	
	return vMap;
}

/**
 * 将传入MapV值转换成VString拼接
 */
function AssemblyStringFromMapV(mapv){
	var vArray = mapv.arr;
	var recommendv='';
	for(var i=0;i<vArray.length;i++){
		recommendv = recommendv +  vArray[i].value+'#';
	}
	return recommendv;
}

/**
 *  解析字串 得到MapV
 */
function AnalyzeStringToMapV(editTableVString,nameArray){
	var map = new CoreMap(); 
	var lineValue = editTableVString.split('#');
	for(var i=0;i<lineValue.length;i++){
		var key = lineValue[0].toString();
		map.put(key,lineValue);
	}
	return map;
}

function CheckAmt(str){
    if(!/^((\d{1,3}(,\d{3})*)|(\d+))(\.\d{1,2})?$/.test(str)) {  
        alert("请输入正确金额数!");
	}
}